﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Application.SystemSecurity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity.ProjectEnt;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Application;
using ZFine.Application.Project;
using ZFine.Domain.Entity;

namespace ZFine.Web.Controllers
{
    public class LoginController : Controller
    {
        private DutyApp dutyApp = new DutyApp();
        private UserApp userapp=new UserApp();
        private PositionRecordApp positoonapp = new PositionRecordApp();

        public Log FileLog
        {
            get { return LogFactory.GetLogger(this.GetType().ToString()); }
        }
        [HttpGet]
        public virtual ActionResult Index()
        {
            var test = string.Format("{0:E2}", 1);
            return View();
        }
        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }


        public ActionResult Paicheinde()
        {
            Dictionary<string,List<UserEntity>> thisuserandpotion=new Dictionary<string, List<UserEntity>>();
            var thispos = positoonapp.GetForm(f => f.State == true);
            ViewData["thispos"] = thispos;
            var data = dutyApp.GetList();
            if (data != null && data.Count > 0)
            {
                foreach (var item in data)
                {
                    var userlist = userapp.GetList(f => f.F_DutyId == item.F_Id&&f.F_DeleteMark!=true);
                    if (userlist != null && userlist.Count > 0)
                    {
                        thisuserandpotion.Add(item.F_FullName, userlist);
                    }
                }
            }
            ViewData["thisuserandpotion"] = thisuserandpotion;
            //var zhiweilist=
            return View();
        }
        [HttpGet]
        public ActionResult OutLogin()
        {
            new LogApp().WriteDbLog(new LogEntity
            {
                F_ModuleName = "系统登录",
                F_Type = DbLogType.Exit.ToString(),
                F_Account = OperatorProvider.Provider.GetCurrent().UserCode,
                F_NickName = OperatorProvider.Provider.GetCurrent().UserName,
                F_Result = true,
                F_Description = "安全退出系统",
            });
            Session.Abandon();
            Session.Clear();
            OperatorProvider.Provider.RemoveCurrent();
            OperatorProvider.Provider.removePositionrecord();
            return RedirectToAction("Index", "Login");
        }

   

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CheckLogin(string username, string password, string code)
        {
            LogEntity logEntity = new LogEntity();
            logEntity.F_ModuleName = "系统登录";
            logEntity.F_Type = DbLogType.Login.ToString();
            try
            {
                //if (Session["ZFine_session_verifycode"].IsEmpty() || Md5.md5(code.ToLower(), 16) != Session["ZFine_session_verifycode"].ToString())
                //{
                //    throw new Exception("验证码错误，请重新输入");
                //}

                UserEntity userEntity = new UserApp().CheckLogin(username, password);
                if (userEntity != null)
                {
                    OperatorModel operatorModel = new OperatorModel();
                    operatorModel.UserId = userEntity.F_Id;
                    operatorModel.UserCode = userEntity.F_Account;
                    operatorModel.UserName = userEntity.F_RealName;
                    operatorModel.CompanyId = userEntity.F_OrganizeId;
                    operatorModel.DepartmentId = userEntity.F_DepartmentId;
                    operatorModel.RoleId = userEntity.F_RoleId;
                    operatorModel.LoginIPAddress = Net.Ip;
                    operatorModel.LoginIPAddressName = Net.GetLocation(operatorModel.LoginIPAddress);
                    operatorModel.LoginTime = DateTime.Now;
                    operatorModel.LoginToken = DESEncrypt.Encrypt(Guid.NewGuid().ToString());
                    operatorModel.IsSystem = Convert.ToBoolean(userEntity.F_IsAdministrator);
                    if (userEntity.F_Account == "admin")
                    {
                        operatorModel.IsSystem = true;
                    }
                    //else
                    //{
                    //    operatorModel.IsSystem = false;
                    //}
                    OperatorProvider.Provider.AddCurrent(operatorModel);
                    logEntity.F_Account = userEntity.F_Account;
                    logEntity.F_NickName = userEntity.F_RealName;
                    logEntity.F_Result = true;
                    logEntity.F_Description = "登录成功";
                    new LogApp().WriteDbLog(logEntity);
                }

                var zhiwei = dutyApp.GetFormNo(userEntity.F_DutyId);
                //var positionrecord = positoonapp.GetForm(f => f.State == true);
                //if (positionrecord != null)
                //{
                //    OperatorProvider.Provider.addPositionrecord(positionrecord.ToJson());
                //}
                if (zhiwei != null && zhiwei.F_FullName.Contains("调度员"))
                {
                    return Content(new AjaxResult { state = ResultType.diaodupaiban.ToString(), message = "登录成功。" }.ToJson());
                }
                else
                {
                    return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。" }.ToJson());
                }
            }
            catch (Exception ex)
            {
                logEntity.F_Account = username;
                logEntity.F_NickName = username;
                logEntity.F_Result = false;
                logEntity.F_Description = "登录失败，" + ex.Message;
                new LogApp().WriteDbLog(logEntity);
                FileLog.Error(ex);
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            }
        }
    }
}
